﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MipsPipelineSimulator
{
    public partial class RegisterSetting : Form
    {
        private MainForm mainForm = null;

        private RegisterSetting()
        {
            InitializeComponent();
        }

        public RegisterSetting(MainForm mainForm)
        {
            this.mainForm = mainForm;
            InitializeComponent();
        }

        private void RegisterSetting_Load(object sender, EventArgs e)
        {
            typeComboBox.SelectedIndex = 4;
        }

        private void RegisterSetting_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void okButton_Click(object sender, EventArgs e)
        {
            switch (typeComboBox.SelectedIndex)
            {
                case 0:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.SByte, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.SByte);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.SByte, strValue.Substring(14), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.SByte, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 1:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.Byte, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.Byte);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.Byte, strValue.Substring(14), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.Byte, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 2:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.Int16, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.Int16);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.Int16, strValue.Substring(12), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.Int16, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 3:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.UInt16, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.UInt16);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.UInt16, strValue.Substring(12), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.UInt16, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 4:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.Int32, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.Int32);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.Int32, strValue.Substring(8), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.Int32, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 5:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.UInt32, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.UInt32);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.UInt32, strValue.Substring(8), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.UInt32, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 6:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.Int64, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.Int64);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.Int64, strValue.Substring(0), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.Int64, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 7:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.UInt64, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.UInt64);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.UInt64, strValue.Substring(0), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.UInt64, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 8:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;

                            }

                            object num = CommonTool.StrToNum(TypeCode.Single, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.Single);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.Single, strValue.Substring(8), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.Single, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
                case 9:
                    {
                        if (!mainForm.commandForm.registerControl.IfExsit(registerNameTextBox.Text))
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "该寄存器不存在!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (registerValueTextBox.Text == null || registerValueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }

                            object num = CommonTool.StrToNum(TypeCode.Double, registerValueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "寄存器值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.registerControl.SetRegisterValue(registerNameTextBox.Text, num, TypeCode.Double);
                        }
                        else
                        {
                            string strValue = mainForm.commandForm.registerControl.GetRegisterValue(registerNameTextBox.Text);
                            object num = CommonTool.StrToNum(TypeCode.Double, strValue.Substring(0), 16);
                            string strResult = CommonTool.NumToStr(TypeCode.Double, num, "D", false);
                            registerValueTextBox.Text = strResult;
                        }
                        break;
                    }
            }
        }
    }
}
